<?php

namespace SisGG\FinalBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use SisGG\FinalBundle\Form\ProveedorType;
use SisGG\FinalBundle\Entity\Empresa;
use SisGG\FinalBundle\Entity\Proveedor;
use SisGG\FinalBundle\Form\CiudadType;
use SisGG\FinalBundle\Form\ProvinciaType;


/**
 * Description of ProveedorController
 *
 * @author sergio
 */
class ProveedorController extends Controller {

    public function indexAction() {
        return new Response('Hello world!');
    }

    public function nuevoProveedorAction() {
        $form = $this->createForm(new ProveedorType());
        $formCiudad=$this->createForm(new CiudadType());
        $formvPro=$this->createForm(new ProvinciaType());
        $provincias = $this->getDoctrine()->getEntityManager()->getRepository("SisGGFinalBundle:Provincia")->findAll();
        return $this->render('SisGGFinalBundle:Proveedor:nuevoProveedor.html.twig', array('form' => $form->createView(), 'error' => null, 'provincias'=>$provincias, 'formCiudad'=>$formCiudad->createView(), 'formProv'=> $formvPro->createView()));
    }

    public function altaProveedorAction(Empresa $empresa = null) {
        $empresa = $this->getEmpresa();
        $proveedor = new Proveedor();
        $formulario = $this->createForm(new ProveedorType(), $proveedor);
        $formulario->bindRequest($this->getRequest());
        if ($formulario->isValid()) {
            $r = $empresa->addProveedor($proveedor, $this);
            if ($r == null) {
                return $this->redirect($this->generateUrl('proveedores', array('msj'=>"El ".$proveedor->getRazonSocial()." CUIT: ".$proveedor->getCuit()." ha sido exitosamente.")));
            } else {
                return $this->render('SisGGFinalBundle:Proveedor:nuevoProveedor.html.twig', array('form' => $formulario->createView(), 'error' => $r));
            }
        }
        return $this->render('SisGGFinalBundle:Proveedor:nuevoProveedor.html.twig', array('form' => $formulario->createView(), 'error' => null));
    }
    
    public function proveedoresAction(Empresa $empresa =null){
        $msj=$this->getRequest()->get('msj');
        $empresa = $this->getEmpresa();
        
        $proveedores = $empresa->getProveedores();
        $parameters = array('proveedores' => $proveedores,'msj' =>$msj, 'criterio' => null,'busqueda' => null, 'direccion' => null, 'id' => null);
        return $this->render('SisGGFinalBundle:Proveedor:proveedores.html.twig', $parameters);
    }

    /**
     * Get empresa
     *
     * @return SisGG\FinalBundle\Entity\Empresa 
     */
    public function getEmpresa() {
        $em = $this->getDoctrine()->getEntityManager();
        $empresa = $em->getRepository('SisGGFinalBundle:Empresa')->find(1);
        return $empresa;
    }

}

?>